<?php
session_start();
require_once 'include/db.php';
require_once 'include/userClass.php';
require_once 'include/postClass.php';
$post = new postClass ($user -> getUserID(), $db);
if(isset($_POST['return'])){
		header ("Location: showPost.php?id={$_POST['id']}");
	}
if (isset ($_POST['post'])) {
	$post -> editPost($_POST, $user -> getUserID());
	header ("Location: showPost.php?id={$_POST['id']}");
	exit ();
}

$userPost = array();
if(isset($_GET['postId'])){
	$userPost = $post -> getEditInfo($_GET['postId']);
}

$title = "Rediger innlegg"; 
include 'include/header.php';

echo "<div id='content'>";
if(!$user -> userLoggedOn() || $user->userName != $post->getAuthor($_GET['postId'])) {
		echo "</br><h4>Oooops, du prøver å redigere et innlegg du ikke eier...</h4></div>";
}
else {
?>
		<h2>Rediger innlegg</h2>
				<!-- Contains previous entered text -->
		<form class="post" method="post" action="editPost.php">
		<label for="postTitle" >Tittel</label>  
		<input type="text" name="postTitle" value="<?php echo $userPost[0]; ?>" required /> <br/>
				<input type="hidden" name="id" value="<?php echo $_GET['postId'] ?>"/>
	    <textarea name ="post"><?php echo $userPost[1]; ?></textarea>
	    <input type ="Submit" value="Lagre innlegg"></input> 
	    </form>
	    <form class="postback" method="post" action='editpost.php'>
	    	<input type="hidden" name="id" value="<?php echo $_GET['postId'] ?>"/>
   		 	<input type='submit' value='Tilbake' name='return'></input>
   		</form>
	    </div>
	    <div id="side">
			<?php
			$user -> showUserInfo($user -> getUserInfo($_GET['postId']));
				if($user -> userLoggedOn() && $user->userName == $post->getAuthor($_GET['postId'])){
						if($user->isUserAdmin() == true) {
						echo "<a class='button' href='adminStat.php?usrName=".$user->userName."' title='Statistikk'>Brukeroversikt</a><br/>\n
						<a class='button' href='newAdmin.php?usrName=".$user->userName."' title='Ny admin'>Ny administrator</a><br/>
						<a class='button' href='displayUsr.php?usrName=".$user->userName."&own=true' title='Min blogg'>Min blogg</a><br/>";
				}
				echo "<a class='button' href='newPost.php?usrName=".$user->userName."' title='Nytt innlegg'><img class='icon' src='include/img/Edit.png' />Nytt innlegg</a><br/>\n
				<a class='button' href='editUser.php' title='Rediger profil'><img class='icon' src='include/img/Wheel.png' />Rediger profil</a>";
			}
			?>			
	    </div>
<?php
}
include 'include/footer.php';?>

